package net.zhengxinyun.performance.service.workSupervision;

import com.sirdc.modules.utils.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.data.WorkSupervisionData;
import net.zhengxinyun.performance.entity.WorkScoreEntity;
import net.zhengxinyun.performance.entity.WorkSupervisionEntity;
import net.zhengxinyun.performance.filter.WorkSupervisionFilter;
import net.zhengxinyun.performance.mapper.workSupervision.WorkScoreEntityMapper;
import net.zhengxinyun.performance.mapper.workSupervision.WorkSupervisionEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by hyp1202 on 2018/12/12.
 */
@Service
public class WorkSupervisionServiceImpl implements WorkSupervisionService {

    private WorkSupervisionEntityMapper workSupervisionEntityMapper;
    private WorkScoreEntityMapper workScoreEntityMapper;
    private WorkSupervisionData workSupervisionData;

    @Autowired
    public WorkSupervisionServiceImpl(WorkSupervisionEntityMapper workSupervisionEntityMapper, WorkScoreEntityMapper workScoreEntityMapper, WorkSupervisionData workSupervisionData) {
        this.workSupervisionEntityMapper = workSupervisionEntityMapper;
        this.workScoreEntityMapper = workScoreEntityMapper;
        this.workSupervisionData = workSupervisionData;
    }

    @Override
    public String insert(WorkSupervisionEntity workSupervisionEntity) {
        int i = workSupervisionEntityMapper.insert(workSupervisionEntity);
        if (i == 0){
            return "添加失败,请联系管理员";
        }
        return "添加成功";
    }

    @Override
    @Transactional
    public String delete(WorkSupervisionEntity workSupervisionEntity) {
        int a = workScoreEntityMapper.deleteByIndicators(workSupervisionEntity.getId());
        if (a == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "指标下的分数删除失败，请重试";
        }
        int i = workSupervisionEntityMapper.deleteByPrimaryKey(workSupervisionEntity.getId());
        if (i == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "删除失败，请重试";
        }
        return "删除成功";
    }

    @Override
    public String update(WorkSupervisionEntity workSupervisionEntity) {
        int i = workSupervisionEntityMapper.updateByPrimaryKeySelective(workSupervisionEntity);
        if (i == 0){
            return "修改失败，请联系管理员";
        }
        return "修改成功";
    }

    @Override
    public List<WorkSupervisionFilter> query(WorkSupervisionFilter filter) {
        List<WorkSupervisionFilter> list = workSupervisionEntityMapper.query(filter);
        for (WorkSupervisionFilter workSupervisionFilter:list) {
            if (StringUtils.isNotBlank(workSupervisionFilter.getPoint())){
                workSupervisionFilter.setScore(Double.valueOf(workSupervisionFilter.getPoint()));
            }else {
                continue;
            }
        }
        return list;
    }

    @Override
    public Integer queryForCount(WorkSupervisionFilter filter) {
        return workSupervisionEntityMapper.queryForCount(filter);
    }



    @Override
    @Transactional
    public String setPoint(WorkSupervisionEntity workSupervisionEntity) {
        workSupervisionEntity.setAudit("0");
        int i = workSupervisionEntityMapper.updateByPrimaryKeySelective(workSupervisionEntity);
        if (i == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "设置分数失败，请重试";
        }
        return "设置分数成功";
    }

    @Override
    @Transactional
    public String importData(MultipartFile file) {
        if (file.getOriginalFilename().endsWith(".xls") == false && file.getOriginalFilename().endsWith(".xlsx") == false) {
            return "文件后缀不对";
        }
        String filePath;
        try {
            Util.write(file.getOriginalFilename(), file.getInputStream());
            filePath = Util.PATH+ file.getOriginalFilename();
            filePath = new String(filePath.getBytes("utf-8"), "utf-8");
            if (StringUtils.isBlank(filePath)) {
                return "filePath为空";
            }
            List<WorkSupervisionEntity> list = workSupervisionData.importWorkSupervision(filePath);
            for (WorkSupervisionEntity entity:list) {
                int i = workSupervisionEntityMapper.insert(entity);
                if (i == 0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return "导入失败";
                }
            }
            return "导入成功";
        } catch (Exception e) {
            return "导入失败！";
        }
    }

    @Override
    public String importPoint(MultipartFile file) {
        if (file.getOriginalFilename().endsWith(".xls") == false && file.getOriginalFilename().endsWith(".xlsx") == false) {
            return "文件后缀不对";
        }
        String filePath;
        try {
            Util.write(file.getOriginalFilename(), file.getInputStream());
            filePath = Util.PATH + file.getOriginalFilename();
            filePath = new String(filePath.getBytes("utf-8"), "utf-8");
            if (StringUtils.isBlank(filePath)) {
                return "filePath为空";
            }
            List<WorkSupervisionEntity> list = workSupervisionData.importWorkSupervision(filePath);
            for (WorkSupervisionEntity entity:list) {
                int i = workSupervisionEntityMapper.updateForPoint(entity);
                if (i == 0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return "导入失败，请重试";
                }
            }
            return "导入成功";
        }catch (Exception e) {
            return "导入失败！";
        }
    }


}
